package com.jyxd.web.controller.dictionary;

import com.jyxd.web.data.dictionary.MonitorManufacturersDictionary;
import com.jyxd.web.service.dictionary.MonitorManufacturersDictionaryService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "监护仪厂家字典相关接口")
@Controller
@RequestMapping(value = "/MonitorManufacturersDictionary")
public class MonitorManufacturersDictionaryController {

    private static Logger logger= LoggerFactory.getLogger(MonitorManufacturersDictionaryController.class);

    @Autowired
    private MonitorManufacturersDictionaryService monitorManufacturersDictionaryService;

    /**
     * 新增一条监护仪字典表数据
     * @param
     * @return
     */
    @ApiOperation("新增一条厂家字典数据")
    @RequestMapping(value = "/insert",method= RequestMethod.POST)
    @ResponseBody
    public String insert(@RequestBody MonitorManufacturersDictionary monitormanufacturersdictionary){
        JSONObject json=new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","添加失败");
        monitormanufacturersdictionary.setId(UUIDUtil.getUUID());
        monitorManufacturersDictionaryService.insert(monitormanufacturersdictionary);
        json.put("code",HttpCode.OK_CODE.getCode());
        json.put("msg","添加成功");
        return json.toString();
    }
    /**
     * 查询厂家字典数据
     * @param
     * @return
     */
    @ApiOperation("查询厂家字典数据")
    @RequestMapping(value = "/getList",method= RequestMethod.POST)
    @ResponseBody
    public String getNoBedMonitorList(@RequestBody Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","暂无数据");
        if(map!=null && map.containsKey("start")){
            int totalCount =monitorManufacturersDictionaryService.getNum(map);
            map.put("start",((int)map.get("start")-1)*(int)map.get("size"));
            json.put("totalCount",totalCount);
        }
        List<MonitorManufacturersDictionary> list =monitorManufacturersDictionaryService.getList(map);
        if(list!=null && list.size()>0){
            json.put("msg","查询成功");
            json.put("data",JSONArray.fromObject(list));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }


    /**
     * 修改厂家字典数据
     * @param
     * @return
     */
    @ApiOperation("修改厂家字典数据")
    @RequestMapping(value = "/updateByid",method= RequestMethod.POST)
    @ResponseBody
    public String updateByid(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","更新失败");
        boolean flag = monitorManufacturersDictionaryService.updateByid(map);
        if(flag){
            json.put("msg","更新成功");
            json.put("data",JSONArray.fromObject(flag));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 修改厂家字典数据
     * @param
     * @return
     */
    @ApiOperation("删除厂家字典数据")
    @RequestMapping(value = "/deleteByid",method= RequestMethod.POST)
    @ResponseBody
    public String deleteByid(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","删除失败");
        boolean flag = monitorManufacturersDictionaryService.deleteByid(map);
        if(flag){
            json.put("msg","删除成功");
            json.put("data",JSONArray.fromObject(flag));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
    /**
     * 修改厂家字典开关启用/禁用
     * @param
     * @return
     */
    @ApiOperation("启用禁用厂家字典数据")
    @RequestMapping(value = "/updateByidAndStatus",method= RequestMethod.POST)
    @ResponseBody
    public String updateByidAndStatus(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","操作失败");
        boolean flag = monitorManufacturersDictionaryService.deleteByid(map);
        if(flag){
            json.put("msg","操作成功");
            json.put("data",JSONArray.fromObject(flag));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
